package com.fulihui.systemcore.service;

import com.fulihui.systemcore.api.VisitRecordService;
import com.fulihui.systemcore.dto.VisitRecordDTO;
import com.fulihui.systemcore.repository.VisitRecordRepository;
import com.fulihui.systemcore.request.callcenter.VisitRecordCreateRequest;
import com.fulihui.systemcore.request.callcenter.VisitRecordQueryRequest;
import com.fulihui.systemcore.request.callcenter.VisitRecordUpdateRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.request.TPageRequest;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author leeson on 2016/6/6.
 */
@Service(version = "1.0.0")
public class VisitRecordServiceImpl implements VisitRecordService {
    @Autowired
    private VisitRecordRepository visitRecordRepository;
//    @Resource
//    private GoodFeedbackService goodFeedbackService;
    @Override
    public TSingleResult<VisitRecordDTO> querySingle(TRequest<Long> request) {
        Integer id = request.getCondition().intValue();
        return ResultBuilder.succTSingle(visitRecordRepository.querySingle(id));
    }

    @Override
    public BaseResult create(TRequest<VisitRecordCreateRequest> request) {
        VisitRecordCreateRequest condition = request.getCondition();

//        //// FIXME: 2017/5/18 为了不让前端发包，临时方案，VisitRecordService#create有益反馈,直接调用information中的服务
//        GoodFeedbackCreateRequest goodFeedbackCreateRequest = new GoodFeedbackCreateRequest();
//        BeanUtils.copyProperties(condition, goodFeedbackCreateRequest);
//        goodFeedbackService.create(goodFeedbackCreateRequest);
        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public BaseResult update(TRequest<VisitRecordUpdateRequest> request) {
        VisitRecordUpdateRequest condition = request.getCondition();


        VisitRecordDTO record = new VisitRecordDTO();
        BeanUtils.copyProperties(condition, record);
        record.setId(condition.getRecordId());

        visitRecordRepository.update(record);

        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public TPageResult<VisitRecordDTO> queryPage(TPageRequest<VisitRecordQueryRequest> request) {
        VisitRecordQueryRequest condition = request.getCondition();

        List<VisitRecordDTO> visitRecordList = visitRecordRepository.queryPage(
                condition.getNickName(), condition.getPhone(), condition.getStartTime(),
                condition.getEndTime(), request.start4Mysql(), request.getRows());

        int totalCount = visitRecordRepository.queryPageCount(condition.getNickName(),
                condition.getPhone(), condition.getStartTime(), condition.getEndTime());

        return ResultBuilder.succTPage(visitRecordList, request.getPage(), request.getRows(),
                totalCount);
    }

    @Override
    public TMultiResult<VisitRecordDTO> queryAll(TRequest<VisitRecordQueryRequest> request) {
        VisitRecordQueryRequest condition = request.getCondition();

        List<VisitRecordDTO> visitRecordList = visitRecordRepository.queryAll(
                condition.getNickName(), condition.getPhone(), condition.getStartTime(),
                condition.getEndTime());

        return ResultBuilder.succTMulti(visitRecordList);
    }
}
